MyQuests.OnCommReceive = {}

--[[ ********************************************************************* --]]
--[[ Received when another MyQuests user targets you.  Returns the numer   --]]
--[[   of quests available for the requesting user to accept.              --]]
--[[ ********************************************************************* --]]
function MyQuests.OnCommReceive:Ping(prefix, sender, distribution, message)
  MyQuests:Debug("Ping", sender, message);

  --local tableOut = {};

  if (message == UnitName("player")) then
    local questCount = table.getn(self.db.char.QuestDiary);
    --for i=1, questCount, 1 do
    --  tableOut[i].id = self.db.char.QuestDiary[i].id;
    --  tableOut[i].title = self.db.char.QuestDiary[i].title;
    --end
  end
  
  self:SendCommMessage("WHISPER", sender, "PingReply", questCount);  
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests.OnCommReceive:PingReply(prefix, sender, distribution, message)
  MyQuests:Debug("PingReply", sender, message);
  self:TriggerEvent("MYQUESTS_MSG_PING_REPLY", sender, message);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests.OnCommReceive:RequestQuestListing(prefix, sender, distribution, message)
  MyQuests:Debug("RequestQuestListing", sender, message);
  
  -- TODO: filter out appropriate quests
  local tableOut = self.db.char.QuestDiary;

  --self:TriggerEvent("MYQUESTS_MSG_LIST_REQUEST", sender, message);
  self:SendCommMessage("WHISPER", sender, "QuestListing", tableOut);    
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests.OnCommReceive:QuestListing(prefix, sender, distribution, message)
  MyQuests:Debug("QuestListing", sender, message);

  self:TriggerEvent("MYQUESTS_MSG_LIST_RECEIVED", sender, message);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests.OnCommReceive:RequestQuest(prefix, sender, distribution, message)
  MyQuests:Debug("RequestQuest", sender, message);

  local questCount = table.getn(self.db.char.QuestDiary);
  for i=1, questCount, 1 do
    if (self.db.char.QuestDiary[i].id == message) then
      self:SendCommMessage("WHISPER", sender, "QuestInfo", self.db.char.QuestDiary[i]);
      break;
    end
  end
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests.OnCommReceive:QuestInfo(prefix, sender, distribution, message)
  MyQuests:Debug("QuestInfo", sender, message);

  self:TiggerEvent("MYQUESTS_MSG_QUEST_INFO", sender, message);
end

--[[ ********************************************************************* --]]
--[[ Function: TurninRequest                                               --]]
--[[   Received when a quester is attempting to turn in a quest.           --]]
--[[ ********************************************************************* --]]
function MyQuests.OnCommReceive.TurninRequest(prefix, sender, distribution, message)
  MyQuests:Debug("TurninRequest", sender, message);

  MyQuests:TriggerEvent("MYQUESTS_TURNIN_REQUEST", sender, message);
end

--[[ ********************************************************************* --]]
--[[ Function: TurninAccepted                                              --]]
--[[   Received by the quester when the turnin player has accepted the     --]]
--[[   turnin request.                                                     --]]
--[[ ********************************************************************* --]]
function MyQuests.OnCommReceive.TurninAccepted(prefix, sender, disrtibution, message)
  MyQuests:Debug("TurninAccepted", sender, message);

  MyQuests:TriggerEvent("MYQUESTS_TURNIN_ACCEPTED", sender, message);
end